// pages/list/list.js
const apiserverUrl = require('../../config').apiserverUrl;
const ExamListUrl = require('../../config').ExamListUrl;
const CheckExamEnableUrl = require('../../config').CheckExamEnableUrl;
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ userInfo: app.globalData.userInfo });
    this.getList();
  },
  exam_click: function (e) {
    var papersid = e.currentTarget.dataset.papersid;
    wx.request({
      method: "post",//请求数据用get，添加数据post，修改数据put
      url: apiserverUrl,
      data: {
        method: "GET",
        url: CheckExamEnableUrl,
        data: {
          papersid: papersid,
          userid: this.data.userInfo.userid
        }
      },
      header: {
        "Content-Type": "application/json"
      },
      success: res => {
        if (res.data.IsSuccess) {
          if (res.data.Data == 1) {
            wx.navigateTo({
              url: '../test/test?papersid=' + papersid,
            });
          } else if (res.data.Data == 2) {
            wx.showModal({
              content: '考试未开始',
              showCancel: false
            });
          } else if (res.data.Data == 3) {
            if (this.data.userInfo.isadmin) {
              wx.navigateTo({
                url: '../userList/userList?papersid=' + e.currentTarget.dataset.papersid,
              });
            } else {
              wx.showModal({
                content: '考试已结束',
                showCancel: false
              });
            }
          } else {
            if (this.data.userInfo.isadmin) {
              wx.navigateTo({
                url: '../userList/userList?papersid=' + e.currentTarget.dataset.papersid,
              });
            } else {
              wx.showModal({
                content: '您已参加过该考试',
                showCancel: false
              });
            }
          }
        }
      }
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getList: function () {
    wx.request({
      method: "post",//请求数据用get，添加数据post，修改数据put
      url: apiserverUrl,
      data: {
        method: "GET", url: ExamListUrl, data: {}
      },
      header: {
        "Content-Type": "application/json"
      },
      success: res => {
        if (res.data.IsSuccess) {
          this.setData({ list: res.data.Data });
        }
      }
    });
  }
})